package com.twu.biblioteca;

import com.twu.biblioteca.Data.Messages;
import com.twu.biblioteca.Data.Options;
import com.twu.biblioteca.Data.UsersList;
import com.twu.biblioteca.IO.IoHandler;
import com.twu.biblioteca.Models.Book;
import com.twu.biblioteca.Models.User;

/**
 * User: haruo
 * Date: 5/17/13
 */
public class BibliotecaMenu {
    private static User user = null;

    public static void selectOption(IoHandler io, LibraryCollection library, String option) {
        switch (Options.convertToOption(option)) {
            case LIST_BOOKS: {
                printBooksList(io, library);
                break;
            }
            case RESERVE_BOOK: {
                reserveBook(io, library);
                break;
            }
            case CHECK_MEMBERSHIP: {
                if (user != null) {
                    printUserMembershipDetails(io);
                } else {
                    io.print(Messages.TALK_TO_LIBRARIAN);
                }
                break;
            }
            case LIST_MOVIES: {
                printMoviesList(io, library);
                break;
            }
            case LOGIN: {
                doLogin(io);
                break;
            }
            case EXIT_BIBLIOTECA: {
                io.print(Messages.BYE);
                break;
            }
            default: {
                io.print(Messages.INVALID_OPTION);
            }
        }
    }

    private static void printUserMembershipDetails(IoHandler io) {
        io.print("Name: " + user.getName());
        io.print("\nEmail: " + user.getEmail());
        io.print("\nPhone Number: " + user.getPhoneNumber());
    }

    private static void doLogin(IoHandler io) {
        io.print(Messages.INSERT_LOGIN);
        String login = io.getInput();
        io.print(Messages.INSERT_PASSWORD);
        String pass = io.getInput();
        user = new UsersList().findUser(login);
        if (user != null && user.getPassword().equals(pass)) {
            io.print(Messages.LOGGED_IN);
        } else {
            user = null;
            io.print(Messages.INVALID_LOGIN_PASSWORD);
        }
    }

    private static void printMoviesList(IoHandler io, LibraryCollection library) {
        io.print(Messages.LISTING_MOVIES);
        io.print(library.getMovieList());
    }

    private static void printBooksList(IoHandler io, LibraryCollection library) {
        io.print(Messages.LISTING_BOOKS);
        io.print(library.getBookList());
    }

    private static void reserveBook(IoHandler io, LibraryCollection library) {
        io.print(Messages.INSERT_ISBN);
        String bookISBN = io.getInput();
        Book book = library.findBook(bookISBN);
        if (book != null) {
            if (book.reserve()) {
                io.print(Messages.RESERVATION_SUCCESSFUL);
            } else {
            io.print(Messages.BOOK_UNAVAILABLE);
            }
        } else {
            io.print(Messages.BOOK_UNAVAILABLE);
        }
    }

    public static void showMenu(IoHandler io) {
        io.print(Messages.MENU);
    }

    public static void showWelcomeMessage(IoHandler io) {
        io.print(Messages.WELCOME);
    }
}
